<body>
<!-- 导航栏开始 -->
<div class="bjy-admin-nav">
	<i class="fa fa-home"></i> 首页
	&gt;
	权限管理
</div>
<!-- 导航栏结束 -->
<ul id="myTab" class="nav nav-tabs">
   <li class="active">
		 <a href="#home" data-toggle="tab">权限列表</a>
   </li>
   <li>
		<a href="javascript:;" onclick="add()">添加权限</a>
	</li>
</ul>
<div id="myTabContent" class="tab-content">
   <div class="tab-pane fade in active" id="home">
		<table class="table table-striped table-bordered table-hover table-condensed">
			<tr>
				<th>权限名</th>
				<th>权　限</th>
				<th>操　作</th>
			</tr>
			<{volist name="list" id="vo"}>
				<tr>
					<td><{$vo._name}></td>
					<td><{$vo.name}></td>
					<td>
						<{if condition="$vo._level neq 3"}>
						<a href="javascript:;" ruleId="<{$vo.id}>" onclick="add_child(this)">添加子权限</a> |
						<{/if}>
						<a href="javascript:;" ruleId="<{$vo.id}>" ruleName="<{$vo.name}>" ruleTitle="<{$vo.title}>" onclick="edit(this)">修改</a> |
						<a href="javascript:" class="delete" data-id="<{$vo.id}>">删除</a>
					</td>
				</tr>
			<{/volist}>
		</table>
   </div>
</div>

<script>
	//请求修改日志状态
	$('.is_log').click(function(){ // 修改
		
    	var is_log = $(this).attr('info');
    	var myid = $(this).attr('myid');
		var s = $(this);
		
    	$.ajax({
        url: './update_log',
        data: {'is_log':is_log, 'id':myid},
        type: 'get',
        dataType: 'jsonp',
        jsonpCallback: 'carlist',
        success: function (data) {
            alert(data.info);
			if(data.info == '成功'){
				if(is_log == 1){
					s.html('是');
					s.attr('info', '0');
					
				}else{
					s.html('否');
					s.attr('info', '1');
					
				}				
			}
        },
        error: function (data) {
            alert(2)
        }
    })
    })

</script>


<!-- 添加菜单模态框开始 -->
<div class="modal fade" id="bjy-add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		 <div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					&times;
				</button>
				<h4 class="modal-title" id="myModalLabel">
					添加权限
				</h4>
			</div>
			<div class="modal-body">
				<input type="hidden" name="pid" value="0">
				<table class="table table-striped table-bordered table-hover table-condensed">
					<tr>
						<th width="12%">权限名：</th>
						<td>
							<input class="form-control" type="text" name="title">
						</td>
					</tr>
					<tr>
						<th>权限：</th>
						<td>
							<input class="form-control" type="text" name="name"> 输入模块/控制器/方法即可 例如 back/rule/index
						</td>
					</tr>
					<tr>
						<th></th>
						<td>
							<input class="btn btn-success submit-add" type="submit" value="添加">
						</td>
					</tr>
				</table>
			</div>
		</div>
	</div>
</div>
<!-- 添加菜单模态框结束 -->

<!-- 修改菜单模态框开始 -->
<div class="modal fade" id="bjy-edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		 <div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					&times;
				</button>
				<h4 class="modal-title" id="myModalLabel">
					修改权限
				</h4>
			</div>
			<div class="modal-body">
				<input type="hidden" name="id">
				<table class="table table-striped table-bordered table-hover table-condensed">
					<tr>
						<th width="12%">权限名：</th>
						<td>
							<input class="form-control" type="text" name="updateTitle">
						</td>
					</tr>
					<tr>
						<th>权限：</th>
						<td>
							<input class="form-control" type="text" name="ruleName"> 输入模块/控制器/方法即可 例如 back/rule/index
						</td>
					</tr>
					<tr>
						<th></th>
						<td>
							<input class="btn btn-success update" type="button" value="修改">
						</td>
					</tr>
				</table>
			</div>
		</div>
	</div>
</div>
<!-- 修改菜单模态框结束 -->
<script>
// 添加菜单
function add(){
	$("input[name='title'],input[name='name']").val(null);
	$("input[name='pid']").val(0);
	$('#bjy-add').modal('show');
}

// 添加子菜单
function add_child(obj){
	$("input[name='pid']").val($(obj).attr('ruleId'));
	$("input[name='title']").val(null);
	$("input[name='name']").val(null);
	$('#bjy-add').modal('show');
}

// 修改菜单
function edit(obj){
	$("input[name='id']").val($(obj).attr('ruleId'));
	$("input[name='updateTitle']").val($(obj).attr('ruletitle'));
	$("input[name='ruleName']").val($(obj).attr('ruleName'));
	$('#bjy-edit').modal('show');
}

$(function(){
    $('.update').click(function(){ // 修改
    	var updateTitle = $("input[name='updateTitle']").val();
    	var ruleName = $("input[name='ruleName']").val();
    	var id = $("input[name='id']").val();

    	if(updateTitle == ''){
    		alert('请填写权限！'); return ;
    	}
    	if(ruleName == ''){
    		alert('请填写权限名！');return;
    	}

    	$.post("<{:url('update_rule')}>",{'updateTitle':updateTitle,'ruleName':ruleName,'id':id},function(data){
            if(data.code){
            	alert(data.msg);
            	location.href="<{:url('rule')}>";
            }else{
                alert(data.msg);
            }
        },'json');
    })

    $('.submit-add').click(function(){ // 添加
    	var title = $("input[name='title']").val();
    	var name = $("input[name='name']").val();
    	var pid = $("input[name='pid']").val();
    	if(title == ''){
    		alert('请填写权限！'); return ;
    	}
    	if(name == ''){
    		alert('请填写权限名！');return;
    	}

    	$.post("<{:url('add_rule')}>",{'title':title,'name':name,'pid':pid},function(data){
            if(data.code){
            	alert(data.msg);
            	location.href="<{:url('rule')}>";
            }else{
                alert(data.msg);
            }
        },'json');
    })

    $('.delete').click(function(){ // 删除
    	if(confirm('确定删除？')){
    		$.post("<{:url('delete_rule')}>",{'id':$(this).data('id')},function(data){
                    if(data.code){
                        alert('删除成功');
                        location.href="<{:url('rule')}>";
                    }else{
                        alert(data.msg);
                    }
            },'json');
    	}
    })
})
</script>
</body>